﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;
using System.IO;


using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Dcjl42")]
    public class Dcjl42Controller : ApiController
    {   

        [HttpPost]
        [Route("PostDcjl42_inq")]

        //多记录查询
        public System.Data.DataSet PostDcjl42_inq([FromBody]  JObject obj)
        {
           
            //查询所有已经提交的记录；【建立索引；】

          


            string WAGON_NO="";

            WAGON_NO = obj["WAGON_NO"] == null ? "" : obj["WAGON_NO"].ToString();

          //  string sql_str = " SELECT * FROM TDCJL42 where  CHECK_STATE ='1' ";

            string sql_str = " SELECT  to_char(to_date(rec_create_time,'yyyymmddhh24miss'),'yyyy-mm-dd')  REC_CREATE_DATE";
            sql_str += ", to_char(to_date(rec_create_time,'yyyymmddhh24miss'),'hh:mm:ss') rec_create_time ";
            sql_str += ",WAGON_NO,REC_CREATOR,GUIDROW  FROM TDCJL42  where  CHECK_STATE ='1'  ";

            if (WAGON_NO.Trim()!="")
            {
                sql_str = sql_str + " and WAGON_NO like '%" + WAGON_NO + "%'";
            }
            sql_str += " order by rec_create_time desc ";
            EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str);           
            outBlock.Tables[0].TableName = "Table0";
            
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;
        }


        [HttpPost]
        [Route("PostDcjl42_Info")]

        //单记录查询
        public System.Data.DataSet PostDcjl42_Info([FromBody]  JObject obj)
        {
            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            string apply_bill_no = obj["GUIDROW"].ToString();

            EI.EIInfo outBlock = new EI.EIInfo();
           
            //1***************************************************************************************TDCJL42
            string sql_str = "  SELECT * FROM TDCJL42  ";
            sql_str  +="  where  GUIDROW='" + apply_bill_no + "'";
            EI.EIInfo outBlock1 = EF.Utility.ExecQuery(sql_str);
            outBlock1.Tables[0].TableName = "TDCJL42";

            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("DCAU", "DCBR", "DCBU", "DCBT", "DCCI", "DCCJ", "DCCL", "DCCN", "DCCO", "DCCK","DCXA");
                // EI.EIInfo codeInfo = EF.Utility.GetCodeClassValue("DCAU", "DCBR", "DCBU", "DCBT", "DCCI", "DCCJ", "DCCL", "DCCM", "DCCN", "DCCO", "DCCK");

            DataTable dtTemp = null;

           
            if (outBlock1.Tables[0].Rows.Count == 1)
            {
                string USE_FLAG = outBlock1.Tables[0].Rows[0]["USE_FLAG"].ToString();
                if (USE_FLAG.Trim() != "")
                {
                    //dtTemp = outBlock_TEMP.Tables["DCAU"].Select("CODE  = '" + USE_FLAG + "'").CopyToDataTable<DataRow>();
                    //if (dtTemp != null && dtTemp.Rows.Count == 1)
                    //    outBlock1.Tables[0].Rows[0]["USE_FLAG"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }


                string EM_STD = outBlock1.Tables[0].Rows[0]["EM_STD"].ToString();
                if (EM_STD.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCBR"].Select("CODE  = '" + EM_STD + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["EM_STD"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string IS_PLATE = outBlock1.Tables[0].Rows[0]["IS_PLATE"].ToString();
                if (IS_PLATE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCBU"].Select("CODE  = '" + IS_PLATE + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["IS_PLATE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                // 

                string IS_QUALIFY = outBlock1.Tables[0].Rows[0]["IS_QUALIFY"].ToString();
                if (IS_QUALIFY.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCBU"].Select("CODE  = '" + IS_QUALIFY + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["IS_QUALIFY"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }
            


                string WAGON_CLASS = outBlock1.Tables[0].Rows[0]["WAGON_CLASS"].ToString();
                if (WAGON_CLASS.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCBT"].Select("CODE  = '" + WAGON_CLASS + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["WAGON_CLASS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }
                                 
                string WAGON_COLOR = outBlock1.Tables[0].Rows[0]["WAGON_COLOR"].ToString();
                if (WAGON_COLOR.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCI"].Select("CODE  = '" + WAGON_COLOR + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["WAGON_COLOR"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }



                string RLLX = outBlock1.Tables[0].Rows[0]["RLLX"].ToString();
                if (RLLX.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCJ"].Select("CODE  = '" + RLLX + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["RLLX"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string SEND_FLAG = outBlock1.Tables[0].Rows[0]["SEND_FLAG"].ToString();
                if (SEND_FLAG.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCL"].Select("CODE  = '" + SEND_FLAG + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["SEND_FLAG"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string OPERATE_FLAG = outBlock1.Tables[0].Rows[0]["OPERATE_FLAG"].ToString();
                if (OPERATE_FLAG.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCN"].Select("CODE  = '" + OPERATE_FLAG + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["OPERATE_FLAG"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string CHECK_STATE = outBlock1.Tables[0].Rows[0]["CHECK_STATE"].ToString();
                if (CHECK_STATE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCO"].Select("CODE  = '" + CHECK_STATE + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["CHECK_STATE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                 

                   string GOV_CHECK_STATE = outBlock1.Tables[0].Rows[0]["GOV_CHECK_STATE"].ToString();
                if (GOV_CHECK_STATE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCCK"].Select("CODE  = '" + GOV_CHECK_STATE + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["GOV_CHECK_STATE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }
                  

                string COMPANY_CODE = outBlock1.Tables[0].Rows[0]["COMPANY_CODE"].ToString();
                if (COMPANY_CODE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["DCXA"].Select("CODE  = '" + COMPANY_CODE + "'").CopyToDataTable<DataRow>();
                    if (dtTemp != null && dtTemp.Rows.Count == 1)
                        outBlock1.Tables[0].Rows[0]["COMPANY_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }
                  

                             
                string rec_create_time = outBlock1.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString();
                if (rec_create_time.Length >= 12) // rec_create_time.Substring(0, 4) + "-" +
                    outBlock1.Tables[0].Rows[0]["REC_CREATE_TIME"] = rec_create_time.Substring(4, 2) + "-" + rec_create_time.Substring(6, 2) + " " + rec_create_time.Substring(8, 2) + ":" + rec_create_time.Substring(10, 2);

                string PROD_DATE = outBlock1.Tables[0].Rows[0]["PROD_DATE"].ToString();
                if (PROD_DATE.Length >= 8) // rec_create_time.Substring(0, 4) + "-" +
                    outBlock1.Tables[0].Rows[0]["PROD_DATE"] = PROD_DATE.Substring(0, 4) + "-" + PROD_DATE.Substring(4, 2) + "-" + PROD_DATE.Substring(6, 2) ;
                	 


            }
                
             
           

           //2***************************************************************************************图片
            // 
          // string localPath = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

            EI.EIInfo outBlock2 = new EI.EIInfo();
            //即使为空，也应压人前台；
            /*
             目前前台仅支持图片，各种图片格式都可以，但不支持word、excle 、pdf等格式，浏览时是黑屏，不影响             
             */
  
            outBlock2.Tables[0].TableName = "FILES";

           
            outBlock2.Tables["FILES"].Columns.Add("FNAME");
            outBlock2.Tables["FILES"].Columns.Add("FNAME_DESC");




            MyFft ftp = new MyFft("10.10.12.150/PHOTO", "ftpuser", "LTTG@menjin123");
           

          
            string  localPath = "D:\\pic\\_pic_new\\dcjl42\\" + apply_bill_no.Substring(2, 4) + "\\"+  apply_bill_no +  "\\";


            if (!Directory.Exists(localPath))
            {
                Directory.CreateDirectory(localPath);
            }

              
            string FileName = apply_bill_no + "_WAGON_PHOTO.jpg";

            if (ftp.FileExist("/", FileName))
            {
                ftp.DownloadFile(FileName, localPath, FileName,true);   
                outBlock2.Tables["FILES"].Rows.Add("http://58.210.172.34:8050/pics/_pic_new/dcjl42/" +  apply_bill_no.Substring(2, 4) + "/"  + apply_bill_no + "/" + FileName,"车头照片");  
            }
  
            FileName =  apply_bill_no + "_XSZ.jpg";

            if (ftp.FileExist("/", FileName))
            {
                ftp.DownloadFile(FileName, localPath, FileName, true);
                outBlock2.Tables["FILES"].Rows.Add("http://58.210.172.34:8050/pics/_pic_new/dcjl42/" + apply_bill_no.Substring(2, 4) + "/" + apply_bill_no + "/" + FileName, "行驶证");  
            }
           
            FileName =  apply_bill_no + "_FDJMP.jpg";
            
            if (ftp.FileExist("/", FileName))
            {
                ftp.DownloadFile(FileName, localPath, FileName, true);
                outBlock2.Tables["FILES"].Rows.Add("http://58.210.172.34:8050/pics/_pic_new/dcjl42/" + apply_bill_no.Substring(2, 4) + "/" + apply_bill_no + "/" + FileName, "发动机铭牌");  
            }
           
            FileName = apply_bill_no + "_SCQD.jpg";
            if (ftp.FileExist("/", FileName))
            {
                ftp.DownloadFile(FileName, localPath, FileName, true);
                outBlock2.Tables["FILES"].Rows.Add("http://58.210.172.34:8050/pics/_pic_new/dcjl42/" + apply_bill_no.Substring(2, 4) + "/" + apply_bill_no + "/" + FileName, "随车清单");
                ////http://58.210.172.37:8019
            }
  
                     
           




         

            //3***************************************************************************************执行结果
            //执行结果
            sql_str  = " select ta.REC_REVISE_TIME, ta.rec_creator ";
            sql_str += " ,decode(length(ta.rec_create_time),14, to_char(to_date(ta.rec_create_time,'yyyymmddhh24miss'),'yyyy-mm-dd hh:mm:ss'),' ') REC_CREATE_TIME ";
            sql_str += " ,ta.remark1 from tpabm29  ta "; 
            sql_str += "  where  ROW_GUID='" + apply_bill_no + "'";
            sql_str += "  order by ta.REC_CREATE_TIME desc ";
            EI.EIInfo outBlock3 = EF.Utility.ExecQuery(sql_str);
            outBlock3.Tables[0].TableName = "TPABM29";

            
              
            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());   
            outBlock.Tables.Add(outBlock3.Tables[0].Copy());



            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }




        [HttpPost]
        [Route("PostDcjl42_pro")]

        //审核、驳回、审核撤销
        public System.Data.DataSet PostDcjl42_pro([FromBody]  JObject obj)
        {



            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();



            inBlock.Tables[0].Columns.Add("CHECK_STATE");    //申请单号   
            inBlock.Tables[0].Columns.Add("GUIDROW");
            inBlock.Tables[0].Columns.Add("VETO_REASON");
            
          

            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code   =   "LT";
            inBlock.sys_info.userid         =   obj["USERID"].ToString();
            inBlock.sys_info.username       =   obj["USERNAME"].ToString();
            inBlock.sys_info.formname       =   obj["FORMNAME"].ToString();


          
            inBlock.Tables[0].Rows[0]["GUIDROW"]        = obj["GUIDROW"] == null ? " " : obj["GUIDROW"].ToString();
            inBlock.Tables[0].Rows[0]["VETO_REASON"]    = obj["VETO_REASON"] == null ? " " : obj["VETO_REASON"].ToString();

            string srv          = obj["SRV"] == null ? " " : obj["SRV"].ToString();
            string check_state  = obj["CHECK_STATE"] == null ? " " : obj["CHECK_STATE"].ToString();

            //if (check_state == "已提交安环部")
            //{
            //    check_state = "1";
            //}


            inBlock.Tables[0].Rows[0]["CHECK_STATE"] = "1";

           
            //srv = "dcjl42_pass";
           //srv = "dcjl42_reback";


            EI.EIInfo outBlock = EI.EITuxedo.CallService(srv, inBlock);



            


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }

    }
}